using Autodesk.Windows;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Xml.Linq;

namespace RibbonCreator
{
    partial class ToolRibbon
    {
        // 尺寸
        private readonly Dictionary<string, uint> ImgSize = new Dictionary<string, uint>()
        {
            { "large",32 },
            { "standard",16 },
        };

        // 创建按钮
        private RibbonButton CreateRibbonButton(Bitmap bitmap, uint size, string btnText, string cmd, RibbonToolTip ribbonToolTip)
        {
            RibbonButton ribbonButton = new RibbonButton
            {
                Text = btnText,
                ToolTip = ribbonToolTip,
                CommandParameter = cmd,
                ShowText = true,
                CommandHandler = new RibbonButtonCmdHandle()
            };
            if (size == ImgSize["large"])
            {
                ribbonButton.Size = RibbonItemSize.Large;
                ribbonButton.LargeImage = Images.GetBitmap(bitmap, size);
                ribbonButton.Orientation = System.Windows.Controls.Orientation.Vertical;
            }
            else
            {
                ribbonButton.Size = RibbonItemSize.Standard;
                ribbonButton.Image = Images.GetBitmap(bitmap, size);
            }

            return ribbonButton;
        }

        // 创建子面板
        private List<RibbonFlowPanel> CreateStandardSubPanel(List<RibbonButton> ribbonButtonList, int columnCount)
        {
            var subPanelList = new List<RibbonFlowPanel>();
            var subPanel = new RibbonFlowPanel();
            subPanel.IsTopJustified = true;

            int i = 1;
            foreach (RibbonButton button in ribbonButtonList)
            {
                subPanel.Items.Add(button);
                if (i % columnCount == 0 || i == ribbonButtonList.Count)
                {
                    subPanelList.Add(subPanel);
                    subPanel = new RibbonFlowPanel();
                    subPanel.IsTopJustified = true;
                }
                i++;
            }

            return subPanelList;
        }

        // 从xml导入界面
        private void LoadPanelsFromXml(string xmlPath)
        {
            var document = XDocument.Load(xmlPath);

            var root = document.Element("ribbon-tab");
            this.ribbonTab.Id = root?.Attribute("id")?.Value ?? "";
            this.ribbonTab.Title = root?.Attribute("title")?.Value ?? "";

            var panelNodes = root?.Elements("panel");
            foreach (var panelNode in panelNodes)
            {
                // 面板名称
                var panelName = panelNode.Attribute("name")?.Value ?? "";
                // 面板实例
                var panel = new RibbonPanel();
                RibbonPanelSource panelSrc = new RibbonPanelSource
                {
                    Title = panelName,
                };
                panel.Source = panelSrc;

                // 子面板集合
                var subPanelNodes = panelNode.Elements("sub-panel");
                foreach (var subPanelNode in subPanelNodes)
                {
                    // 子面板尺寸
                    var sizeString = subPanelNode.Attribute("size")?.Value ?? "standard";
                    ImgSize.TryGetValue(sizeString, out uint size);
                    // 子面板每行btn数量
                    int.TryParse(subPanelNode.Attribute("columnCount")?.Value, out int columnCount);
                    columnCount = columnCount <= 0 ? 3 : columnCount;

                    // 子面板下按钮集合
                    var btnNodes = subPanelNode.Elements("button");
                    var btns = new List<RibbonButton>();
                    foreach (var btnNode in btnNodes)
                    {
                        var btnName = btnNode.Attribute("name")?.Value ?? "";
                        var btnCmd = btnNode.Attribute("command")?.Value ?? "";

                        var appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                        var imgPath = btnNode.Element("img")?.Attribute("src")?.Value ?? "";
                        Bitmap img = new Bitmap(16, 16);
                        if (File.Exists(appPath + "\\" + imgPath))
                        {
                            img = (Bitmap)Bitmap.FromFile(appPath + "\\" + imgPath);
                        }

                        var tip = btnNode.Element("tip");
                        var ribbonToolTip = new RibbonToolTip
                        {
                            Title = tip.Attribute("title")?.Value ?? "",
                            Command = btnCmd,
                            Content = tip.Element("content")?.Value ?? "",
                            IsHelpEnabled = false,
                        };

                        var helpSource = appPath + "\\" + tip.Element("a")?.Attribute("href")?.Value ?? "";

                        // 若文件存在，则将文件加入帮助资源中
                        if (File.Exists(helpSource))
                        {
                            ribbonToolTip.IsHelpEnabled = true;
                            ribbonToolTip.HelpSource = new Uri(helpSource);
                        }

                        var btn = CreateRibbonButton(img, size, btnName, btnCmd, ribbonToolTip);
                        btns.Add(btn);
                    }

                    if (size == ImgSize["large"])
                    {
                        foreach (var btn in btns)
                        {
                            panelSrc.Items.Add(btn);
                        }
                    }
                    else
                    {
                        var subPanels = CreateStandardSubPanel(btns, columnCount);
                        foreach (var subPanel in subPanels)
                        {
                            panelSrc.Items.Add(subPanel);
                        }
                    }
                }
                this.ribbonTab.Panels.Add(panel);
            }
        }
    }
}
